package ui.videos 
{
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import utils.ShapeUtil;
	
	/**
	 * ...
	 * @author usual
	 */
	public class SeekAudioBar extends MovieClip 
	{
		private var _bg:Sprite;
		private var _seekBar:Sprite;
		private var _thumBtn:MovieClip;
		private var _transparentBtn:Sprite;
		
		public function SeekAudioBar() 
		{
			addEventListener(Event.ADDED_TO_STAGE, build);
		}
		
		private function build(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, build);
			addSeekAudioBar();
			addTransparentBtn();
			addThumb();
			resize();
		}
		public function mute():void
		{
			var wtr:Number = 0
			_thumBtn.x = 0;
			_seekBar.width = 0;
			MovieClip(parent).updateVol(wtr);
		}
		
		public function unMute():void
		{
			var wtr:Number = 100
			_thumBtn.x = _bg.width;
			_seekBar.width = _thumBtn.x;
			MovieClip(parent).updateVol(wtr);
		}
		private function addTransparentBtn():void 
		{
			_transparentBtn = ShapeUtil.getSprite(_bg.width, _bg.height + 2, 0xff0000, 0);
			_transparentBtn.buttonMode = true;
			_transparentBtn.y = _bg.y - 1;
			addChild(_transparentBtn);
			_transparentBtn.addEventListener(MouseEvent.MOUSE_DOWN, _onClik);
		}
		
		private function _onClik(e:MouseEvent=null):void 
		{
			var wtr:Number = ((_transparentBtn.mouseX * 100 ) / _bg.width);

			_thumBtn.x = _transparentBtn.mouseX;
			_seekBar.width = _thumBtn.x;
			MovieClip(parent).updateVol(wtr);
			trace(wtr + '%');
		}
		
		private function addThumb():void 
		{
			_thumBtn = new MovieClip();
			var bulle:Shape = ShapeUtil.getCircle(3.5, 0x01f2f2);
			_thumBtn.addChild(bulle);
			_thumBtn.buttonMode = true;
			addChild(_thumBtn);
			_thumBtn.addEventListener(MouseEvent.MOUSE_DOWN, _onDown);

		}
		
		private function _onDown(e:MouseEvent):void 
		{
			stage.addEventListener(MouseEvent.MOUSE_UP, _onUp);
			addEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
		}
		
		private function _onMouseMove(e:MouseEvent):void 
		{
			var mx:Number = this.mouseX;
			if ( mx > _bg.x && mx<_bg.width)
			{
				_thumBtn.x = mx;
				_seekBar.width = _thumBtn.x;
				var wtr:Number = ((_seekBar.width * 100 ) / _bg.width);
				trace("[SeekAudioBar._onUp] ", wtr);
				MovieClip(parent).updateVol(wtr);
			}
		}
		
		private function _onUp(e:MouseEvent):void 
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP, _onUp);
			removeEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
			var wtr:Number = ((_seekBar.width * 100 ) / _bg.width);
			trace("[SeekAudioBar._onUp] ", wtr);
			MovieClip(parent).updateVol(wtr);
		}
		
		private function resize():void 
		{
			_thumBtn.y = (_bg.height / 2);
			_thumBtn.x = _bg.x + _bg.width;
		}
		
		public function update():void
		{
			
		}
		
		private function addSeekAudioBar():void 
		{
			_bg = ShapeUtil.getSprite(70, 1, 0xffffff, .5);
			addChild(_bg);
			
			_seekBar = ShapeUtil.getSprite(70, 1, 0x01f2f2, 1);
			addChild(_seekBar);
			
			//_seekBar.width = 1;
		}
		
	}

}